﻿CREATE FUNCTION QIC.InterfaceFactory(@ConfigName VARCHAR(50), @InterfaceId INT)
RETURNS NVARCHAR(MAX)
AS
BEGIN

	DECLARE @DelegateName VARCHAR(50)
	DECLARE @InterfaceType VARCHAR(50)
	DECLARE @InterfaceFactory VARCHAR(50)

	SELECT @InterfaceFactory = InterfaceFactory, @InterfaceType = InterfaceType FROM QIC.Interface_(@ConfigName) WHERE InterfaceId = @InterfaceId

	SET @DelegateName = @InterfaceType

	IF @InterfaceType <> @InterfaceFactory AND EXISTS (SELECT * FROM QIC.Delegate_(@ConfigName) WHERE InterfaceSchemaName = 'QIC' AND InterfaceName = 'InterfaceFactory_' AND DelegateName = @InterfaceFactory)
	BEGIN
		SET @DelegateName = @InterfaceFactory
	END

	RETURN QIC.InterfaceFactory_(@DelegateName,@ConfigName,@InterfaceId)
END

